package com.sysystem.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sysystem.common.Constant;
import com.sysystem.common.DevidePage;
import com.sysystem.enums.BaseRetCode;
import com.sysystem.model.Sign;
import com.sysystem.service.ISignService;
import com.sysystem.vo.BaseResponseVO;

@RestController
public class SignController extends BaseController {
    
    private static Logger logger = LoggerFactory.getLogger(SignController.class);

    @Autowired
    private ISignService signService;
    
    @RequestMapping(value = "/signed", method = RequestMethod.POST)
    public BaseResponseVO signed(@RequestParam(value="train_cd",required=false) String train_cd,
    							@RequestParam(value="pageNo",required=false) String pageNo) {
        try {
            logger.info("---SignController METHOD  signed  START---");
            
            int currentPage = 1;// 当前页是第一页
            if (!StringUtils.isEmpty(pageNo)) {
    			currentPage = Integer.parseInt(pageNo);
    		}
            
            // 总记录条数 
            int recordCount = signService.selectSignedAllDataCount(train_cd);
    		DevidePage pUtil = new DevidePage(Constant.PAGE_SIZE, recordCount, currentPage);
    		int start = pUtil.getFromIndex();
    		int end = pUtil.getToIndex();
    		
    		//总页数
    		int pageCount = pUtil.getPageCount();
    		if (currentPage > pageCount) {
    			// 没有更多的记录
    			return this.buildRespone(BaseRetCode.CODE_ERROR_NO_MOREDATA);
    		}

    		List<Sign> trainList = signService.selectSignedAllData(train_cd, start, end);
            
            logger.info("---SignController METHOD  signed  END");
            
            BaseResponseVO resData = this.buildResultDataRespone(trainList);
            
            return resData;
        } catch(Exception e) {
            e.printStackTrace();
            return this.buildRespone(BaseRetCode.CODE_ERROR_SYS);
        }
    }
    
    @RequestMapping(value = "/sign", method = RequestMethod.POST)
    public BaseResponseVO sign(@RequestParam(value="train_cd",required=false) String train_cd,
    							@RequestParam(value="wechat_cd",required=false) String wechat_cd) {
    	try {
    		logger.info("---SignController METHOD  sign  START---");
    		
    		if (StringUtils.isEmpty(train_cd) || StringUtils.isEmpty(wechat_cd)) {
    			// 引数指定无效
    			return this.buildRespone(BaseRetCode.CODE_ERROR_PARAMATER_ERROR);
    		}
    		
    		int updateedRecordCount = signService.saveSignData(train_cd, wechat_cd);
    		
    		logger.info("---SignController METHOD  sign  END---");
    		
    		// 签到成功
    		if (updateedRecordCount > 0) {
    			return this.buildRespone(BaseRetCode.CODE_SUCCESS);
    		}
    		
    		// 签到未能成功
    		return this.buildRespone(BaseRetCode.CODE_ERROR_SIGN_FAILED);
             
		} catch (Exception e) {
			e.printStackTrace();
            return this.buildRespone(BaseRetCode.CODE_ERROR_SYS);
		}
    }
}
